package assignment1;

//constructor
/**
 *
 * @author rain.divine88
 */
public class MainForm {

    private getSetClass gsClass;
    private UIClass uiClass;

    //constant variables for probability
    private static final float MIN_PROB_VAL = 0.0f;
    private static final float MAX_PROB_VAL = 1.0f;
    private static final int RUN_ONE_CYCLE = 1;

    public MainForm() {
        gsClass = new getSetClass();
        uiClass = new UIClass(this.gsClass);
        uiClass.roughGrid('*');
        uiClass.showGrid();
    }
  
    private  void menuOption() {
        //show menu to user
        System.out.println("=== Menu ===\n" +
        "1. Set number of horizontal lanes <H-street> (min 1, max 3)\n" +
        "2. Set number of vertical lanes <V-street> (min 1, max 4)\n" +
        "3. Set probability of a car entering H-street (min 0, max 1)\n" +
        "4. Set Probability of a car entering V-street (min 0, max 1)\n" +
        "5. Run one simulation cycle\n" +
        "6. Set and run number of simulation cycle (min 1, max 10)\n");
    }    

    public  void runProg(){
        while (true) {            
            menuOption();

            int menuChoice = Integer.parseInt(GetUserInput.getUserInput("Enter your choice: "));
            switch (menuChoice) {
                case 1: System.out.println("Menu 1");
                    runSetNumHLane();
                    uiClass.roughGrid('*');
                    uiClass.showGrid();          
                    break;
                case 2: System.out.println("Menu 2");
                    runSetNumVLane();
                    uiClass.roughGrid('*');
                    uiClass.showGrid();                  
                    break;
                case 3: System.out.println("Menu 3");
                    runSetProbHLane();
                    uiClass.roughGrid('*');
                    uiClass.showGrid();

                    break;
                case 4: System.out.println("Menu 4");
                    runSetProbVLane();
                    uiClass.roughGrid('*');
                    uiClass.showGrid();
                    break;
                case 5: System.out.println("Menu 5");
                    runOneSimulationCycle();                   
                    break;
                case 6: System.out.println("Menu 6");
                    setAndRunSimulationCycle();
                    break;
                default: System.out.println("Error in entering menu option");
                    break;
            }
        }
    }

    //method for menu one
    private void runSetNumHLane(){
        int userInput = Integer.parseInt(GetUserInput.getUserInput("Enter number of Horizontal lanes (min 1, max 3): "));

        if (userInput >= gsClass.getMinHLanes() && userInput <= gsClass.getMaxHLanes()) {
            gsClass.setHLaneNumber(userInput);                 
        }else{
            System.out.println("");
            System.out.println("Error input for number of horizontal lanes");
            System.out.println("Please enter number between 1 and 3 inclusive.");
            runSetNumHLane();
        }
    }

    //method for menu two
    private void runSetNumVLane(){
        int userInput = Integer.parseInt(GetUserInput.getUserInput("Enter number of Vertical lanes (min 1, max 4): "));

        if (userInput >= gsClass.getMinVLanes() && userInput <= gsClass.getMaxVLanes()) {
            gsClass.setVLaneNumber(userInput);                 
        } else {
            System.out.println("");
            System.out.println("Error input for number of vertical lanes");
            System.out.println("Please enter number between 1 and 4 inclusive");
            runSetNumVLane();
        }
    }

    //method for menu three
    private void runSetProbHLane(){       
        float userInput = Float.valueOf(GetUserInput.getUserInput("Enter probability of a car entering H-street (min 0, max 1): ").trim()).floatValue();
        if (userInput >= MIN_PROB_VAL && userInput <= MAX_PROB_VAL) {
            gsClass.setProbHLane(userInput);            
        } else {
            System.out.println("");
            System.out.println("Error input for probability");
            System.out.println("Please enter number between 0 and 1 inclusive");
            runSetProbHLane();
        }
    }

    //method for menu four
    private void runSetProbVLane(){       
        float userInput = Float.valueOf(GetUserInput.getUserInput("Enter probability of a car entering V-street (min 0, max 1): ").trim()).floatValue();
        if (userInput >= MIN_PROB_VAL && userInput <= MAX_PROB_VAL) {
            gsClass.setProbVLane(userInput);            
        } else {
            System.out.println("");
            System.out.println("Error input for probability");
            System.out.println("Please enter number between 0 and 1 inclusive");
            runSetProbVLane();
        }
    }

    //method for menu five
    private void runOneSimulationCycle(){        
        uiClass.moveCars(RUN_ONE_CYCLE);        
    }

    //method for menu six
    private void setAndRunSimulationCycle(){
        int userInput = Integer.parseInt(GetUserInput.getUserInput("How many simulation cycles?: "));

        if (userInput >= gsClass.getMinSimulation() && userInput <= gsClass.getMaxSimulation()) {            
            uiClass.moveCars(userInput);
        } else {
            System.out.println("");
            System.out.println("Error input for number of simulation cycles");
            System.out.println("Please enter number between 1 and 10 inclusive");
            setAndRunSimulationCycle();
        }
    }
}
